package org.maxgroundstation.groundstation.comm.service.list;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.maxgroundstation.groundstation.comm.service.Service;
import org.maxgroundstation.groundstation.comm.service.ServiceCommunication;
import org.maxgroundstation.groundstation.comm.service.ServiceDataFrame;
import org.maxgroundstation.groundstation.comm.service.ServiceUtil;

/**
 * The list service.
 * @author Scott Davis
 * 
 * Copyright (C) 2010  Scott Davis
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class ListService extends Service {

	// Static members
	public static final String SERVICE_ID = ServiceUtil.getServiceID("__list");
	public static final String TYPE = "list";
	
	/**
	 * Constructor
	 * @param comm the service communication.
	 */
	public ListService(ServiceCommunication comm) {
		// Use Service constructor.
		super(comm);
	}

	@Override
	public String getID() {
		return SERVICE_ID;
	}
	
	@Override
	public boolean isSubscribable() {
		return false;
	}
	
	@Override
	public boolean isDefaultService() {
		return true;
	}
	
	@Override
	public void initialize() {
		// Do nothing.
	}
	
	@Override
	public void handleResponse(ServiceDataFrame dataFrame) {
		// Parse XML response.
		String xmlString = dataFrame.getDataString().trim();
		SAXBuilder saxBuilder = new SAXBuilder();
		StringReader reader = new StringReader(xmlString);
		Document doc = null;
		try {
			doc = saxBuilder.build(reader);
		}
		catch (Exception e) {
			e.printStackTrace(System.err);
		}
		
		// Populate list of services.
		if (doc != null) {
			List<Service> serviceList = new ArrayList<Service>();
			
			Element rootElement = doc.getRootElement();
			if (rootElement != null) {
				Iterator i = rootElement.getChildren("service").iterator();
				while (i.hasNext()) {
					Element serviceElement = (Element) i.next();
					String serviceID = serviceElement.getAttributeValue("id");
					String name = serviceElement.getAttributeValue("name");
					String format = serviceElement.getAttributeValue("format");
					
					// Add description if available.
					String description = serviceElement.getChildTextTrim("description");
					if (description == null) description = "";
					
					// Add parameters.
					Map<String, String> parameters = new HashMap<String, String>();
					Iterator j = serviceElement.getChildren("param").iterator();
					while (j.hasNext()) {
						Element paramElement = (Element) j.next();
						String key = paramElement.getAttributeValue("key");
						String value = paramElement.getAttributeValue("value");
						parameters.put(key, value);
					}
					
					// Create service.
					Service service = ServiceUtil.createService(serviceID, getServiceCommunication());
					service.setName(name);
					service.setFormat(format);
					service.setDescription(description);
					service.setParameters(parameters);
					serviceList.add(service);
					System.out.println("Found service: " + serviceID + " - " + name);
				}
			}
			
			getServiceCommunication().setServices(serviceList);
		}
	}

    @Override
    public String getType() {
        return TYPE;
    }
}